<template>
	<view class="content">
		<view class="boxcell" v-if="list.length" v-for="(item, key) in list" :key="key" @click="isSelect(item)">
			
					<view class='topCell'>
						<view class='leftView' >
							<view class='name'>{{item.name}}</view>
							<view class="phone-num">{{item.mobile}}</view>
						</view>

						<image class='editImage' src='/static/image/my/modifyaddress.png' @click="toEdit(item.id)"></image>
					

					</view>
					<view class='bottomView'>
	
						<view class="cell-tip" v-show="item.is_def === 1">默认</view>
						<view class="cell-bd-text">{{item.area_name + item.address}}</view>
				
					</view>
				
		
		</view>

		
		
		<view class="btttonView" @click="toAdd()" >新增收货地址</view>
		
		<view style="width: 100%;height: 100upx;"></view>
			
			<!-- #ifdef MP-WEIXIN -->
			<!-- <button class="btn btn-square btn-b" @click="wechatAddress" hover-class="btn-hover2">从微信获取</button> -->
			<!-- #endif -->


	</view>
</template>

<script>
export default {
    data() {
        return {
			list: [] ,// 用户收货地址列表
			type: ''
        }
    },
	onLoad (e) {
		if(e.type){
			this.type = e.type;
		}
	},
	onShow () {
		this.userShipList();
	},
    methods: {
		// 获取收货地址列表
		userShipList () {
			this.$api.userShip({}, res => {
                if (res.status) {
                    this.list = res.data
										
                }
            })
		},
		// 收货地址删除
		delShip (id) {
			this.$common.modelShow('提示', '确认删除此收货地址?', () => {
				let data = {
					id: id
				}
				this.$api.removeShip(data, res => {
					if (res.status) {
						this.$common.successToShow(res.msg, () => {
							this.userShipList();
						});
					} else {
						this.$common.errorToShow(res.msg);
					}
				})
			})
		},
		//编辑
		toEdit (id) {
			this.$common.navigateTo('./index?ship_id=' + id);
		},
		//添加
		toAdd() {
			this.$common.navigateTo('./index');
		},
		//选择
		isSelect(data) {
			

			
			if(this.type == 'order'){
		// 		let pages = getCurrentPages();//当前页
		// 		let beforePage = pages[pages.length - 2];//上个页面
				
		// 		// #ifdef MP-ALIPAY || MP-TOUTIAO
		// 		this.$db.set('address_user_ship', data, true);
		// 		// #endif
		
		// 		// #ifdef H5 || APP-PLUS || APP-PLUS-NVUE
		// 		this.$store.commit("userShip",data)
		// 		// #endif

		// 		// #ifdef MP-WEIXIN
		// 		beforePage.$vm.userShip = data;
		// 		beforePage.$vm.params.area_id = data.area_id;
		// 		// #endif
		
				this.$db.set('address_user_ship', data, true);
				uni.navigateBack({
					delta: 1
				});
		
				
				// this.$common.navigateTo("/pages/goods/place-order/index")
			}
		},
		// #ifdef MP-WEIXIN
		wechatAddress: function () {
			wx.chooseAddress({
				success: res => {
					if (res.errMsg == "chooseAddress:ok") {
						//获取成功
						//存储这个收获地区信息到数据库
						let data = {
							province_name: res.provinceName,
							city_name: res.cityName,
							county_name: res.countyName,
							postal_code: res.postalCode
						};
						let areaId = 0;
						this.$api.getAreaId(data, res1 => {
							if (res1.status) {
								//存储用户收货信息
								let userShipId = 0;
								// let userShipData = {
								// 	area_id: res1.data,
								// 	user_name: res.userName,
								// 	detail_info: res.detailInfo,
								// 	tel_number: res.telNumber,
								// 	is_def: 2
								// }
								let userShipData = {
									area_id: res1.data,
									name: res.userName,
									address: res.detailInfo,
									mobile: res.telNumber,
									is_def: 2
								}
								this.$api.editShip(userShipData, res2 => {
									if (res2.status) {
										this.$common.errorToShow('存储微信地址成功', r => {
											setTimeout(rp => {
												this.userShipList();
											}, 1000);
										});
									}else{
										uni.showModal({
											title: '提示',
											content: '存储微信地址失败',
											showCancel: false
										});
									}
								});
							}else{
								uni.showModal({
									title: '提示',
									content: '获取微信地址失败',
									showCancel: false
								});
							}
						});
					} else {
						uni.showModal({
							title: '提示',
							content: '获取微信地址失败',
							showCancel: false
						});
					}
				}
			});
		},
		// #endif
    }
}
</script>

<style>

page{
	
	background-color: #F6F6F6;
	
}


.boxcell{
	
	margin-top: 36upx;
	
	margin-left: 36upx;
	
	width: 678upx;
	
	height: 177upx;
	
	background-color: #FFFFFF;
	
	border-radius: 22upx;
	
	
}


.topCell{
	
	width: 100%;
	
	height: 100upx;
	
	
	display: flex;
	
	flex-direction: row;

	justify-content: space-between;
	
		align-items: center;
	
}

.leftView{
	
	width: 60%;
	
	height: 100%;
	
	/* background-color: #007AFF; */
	
	
	display: flex;
	
	flex-direction: row;
	
	align-items: center;
	
}


.name{
	
	font-size: upx;
	
	margin-left: 36upx;
	
	
}


.phone-num{
	
	font-size: 28upx;
	
	margin-left: 24upx;
	
	
	color: #A6A6A6;
	
	
}


.editImage{
	
	width: 34upx;
	
	height: 34upx;
	
	margin-right: 40upx;
	
	
	
}


.bottomView{
	
	width: 100%;
	
	height: 40upx;
	
	align-items: center;
	
	display: flex;

	flex-direction: row;
	
	
	/* background-color: #00BFFF; */
	
}


.cell-tip{
	
	margin-left: 36upx;
	
	width: 72upx;
	
	height: 37upx;
	
	line-height: 37upx;
	
	border-radius: 11upx;
	
	text-align: center;
	
	background-color: #FF5A5F;
	
	color: #FFFFFF;
	
	font-size: 24upx;
	
}

.cell-bd-text{
	
		margin-left: 36upx;
		
		line-height: 37upx;
		
		overflow: hidden;
		-webkit-line-clamp: 1;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
	
}


	.btttonView{
		
	margin-top: 185upx;
	
	margin-left: 36upx;
	
	text-align: center;
	
	width: 678upx;
	
	height: 90upx;
	
	// margin-left: 36upx;
	
	font-size: 36upx;
	
	color: #FFFFFF;
	
	line-height: 90upx;
	
	// border-radius: 45upx;
	
	
	background-color: #FF5A5F;
	
	
	box-shadow:   0px 3upx 7upx rgba(0,0,0,0.06),  0upx 2upx 4upx rgba(0,0,0,0.03),0upx 1upx 2upx rgba(0,0,0,0.03);
	
	
	   
	
	}

</style>